
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>盘点</title>
    <base href="<%=request.getContextPath()%>/"/>
    <link rel="stylesheet" href="webjars/layui/2.5.6/css/layui.css">
</head>
<body>
<form class="layui-form" lay-filter="search" style="margin: 0 auto;width: 90%">
    <div class="layui-form-item layui-inline">
        <label class="layui-form-label">物资名称:</label>
        <div class="layui-input-inline">
            <input type="text" name="cgName" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item layui-inline">
        <label class="layui-form-label">物资编码:</label>
        <div class="layui-input-inline">
            <input type="text" name="cgCode" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item layui-inline">
        <label class="layui-form-label">盘点院区:</label>
        <div class="layui-input-inline">

            <select name="branchCode"  lay-filter="branchCode" lay-verify="">
                <option value="东">东院区</option>
                <option value="西">西院区</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-inline">
        <label class="layui-form-label">盘点仓库:</label>
        <div class="layui-input-inline">
            <select name="warehousId"  lay-filter="warehouseName" lay-verify="" id="warehouseName">
                <option value="0">请选择</option>
            </select>

        </div>
    </div>

    <div class=" layui-form-item layui-inline">
        <button type="button" onclick="search();" class="layui-btn layui-btn-normal"><i class="layui-icon layui-icon-search"></i></button>
    </div>



</form>





<div class="layui-container">

    <table id="stockTb" lay-filter="stockTb"></table>


</div>


<script src="webjars/layui/2.5.6/layui.js"></script>
<script>

    layui.use(["table","form","jquery","layer"],function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.$;
        let layer = layui.layer;
        let params = layui.form.val("search");
        table.render({
            id: "stockTb",
            elem: "#stockTb",
            where: params,
            url: "stock/getStock",
            cols: [[
                {title: "物资编码", field: "cgCode"},
                {title: "物资名称", field: "cgName"},
                {title: "最小规格", field: "spec"},
                {title: "最小单位", field: "unit"},
                {title: "物资分类", field: "type" },
                // {title: "包装单位", field: "packUnit", width: 80},
                // {title: "包装规格", field: "packSpec", width: 80},
                {title: "系统库存", field: "amount" },
                {title: "实际库存", field: "realAmount",width:100,edit:"text"},
                //{title: "操作", toolbar: "#action"}
            ]],
            done:function (res,curr,count) {
                //物资分类的转换
                $("[data-field='type']").children().each(function(){
                    if($(this).text()=='0'){
                        $(this).text("低值")
                    }else if($(this).text()=='1'){
                        $(this).text("高值")
                    }
                });
            },
            page: true
        })
        form.on('select(branchCode)', function(data){
            // console.log(data.elem); //得到select原始DOM对象
            //console.log(data.value); //得到被选中的值
            // console.log(data.othis); //得到美化后的DOM对象
            $.post({
                url: 'wareHouse/getTbUscWarehouseByBranch',
                data: {"branch":data.value},
                //dataType: 'json',
                success: function ({data}) {
                    //使用循环遍历，给下拉列表赋值
                    $('#warehouseName option:not(:first)').remove();
                    $.each(data, function (index, value) {
                        // console.log(value.department_id);
                        //console.log(index,value);
                        $('#warehouseName').append(new Option(value.warehouseName,value.warehousId));// 下拉菜单里添加元素
                    });
                    layui.form.render("select");//重新渲染 固定写法
                }
            })

        });

        table.on('edit(stockTb)', function(obj){
           // console.log(obj.value); //得到修改后的值
            //console.log(obj.field); //当前编辑的字段名
            //console.log(obj.data); //所在行的所有相关数据
            //console.log(obj.tr.selector);
            //console.log(typeof obj.data.realAmount );
            if(isNaN(obj.data.realAmount)){
                layer.msg("请输入数字！");
                return;
            }
            let realAmount = Number(obj.data.realAmount);
            if(realAmount <= 0){
                layer.msg("请输入正确数字！");
                return;
            }
            if(realAmount <= 0){
                layer.msg("请输入正数！");
                return;
            }
            if(realAmount %1 !== 0){
                layer.msg("请输入整数！");
                return;
            }
            layer.confirm('当前库存不一致,是否要修正库存?', {icon: 3, title:'提示'}, function(index){
                $.post({
                    url: 'stock/updateStockAmount',
                    data: {"stockMasterId":obj.data.stockMasterId,"amount":realAmount},
                    //dataType: 'json',
                    success: function ({msg}) {
                        layui.table.reload("stockTb");
                        layer.close(index);
                        layer.msg(msg);
                    }
                })

            });

        });
    })



    //条件模糊查询
    function search() {
        layui.use(["table", "form", "jquery"], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.$;
            let params = layui.form.val("search");
            //console.log(params);
            layui.table.reload('stockTb', {
                where: params
            });

        });
    }



</script>
</body>
</html>
